/**
 *	流程模块接口文件：option数据选项
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.system.dept;



import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framework.Flow.FlowListener;

import java.util.Map;


public class FlowListenerdept extends FlowListener {



    @Override
    public Boolean onModejs() {
        return true;
    }

    public Boolean onRemind() { return false;}

    public Map<String, String> onDatabefore(){
        String pid    = mRequest.get("pid", "0");
        String where  = "";
        if(mRequest.COMPANYBOOL){
             where = "(`pid`="+pid+" or `id`="+pid+")";
        }

        return Rock.getMap(new String[]{
                "where", where,
        });
    }

    public Map<String, String> onDataafter()
    {
        Map<String, String> da = Rock.getMap();
        if(mRequest.COMPANYBOOL) {
            da.put("deptarr", Json.getListString(mFlow.mModel.getAll("`cid`="+mRequest.COMPANYID+"", "`id`,`name`,`pid`", "`sort`")));
        }
        return da;
    }

    public Map<String, String> onSavebefore(Map<String, String> da, String mid, Boolean isadd)
    {
        int pid     = Rock.getMapint(da, "pid");
        String cid  = mRequest.COMPANYID;
        int count;
        if(pid==0) {
            count = mFlow.mModel.getCount("`cid`=" + cid + " and `pid`=0 and `id`<>"+mid+"");
            if(count>0)return mFlow.setErrors("顶级部门已经存在了");
        }
        if(pid>0){
            count = mFlow.mModel.getCount("`cid`=" + cid + " and `id`="+pid+"");
            if(count==0)return mFlow.setErrors("上级ID"+pid+"不存在");

            if(!isadd && mid.equals(""+pid))return mFlow.setErrors("上级ID不能是"+pid+"");
        }

        return null;
    }

    @Override
    public String onDeletebefore() {
        String cid = Rock.getMapString(mFlow.data, "cid");
        int to = mFlow.mModel.getCount("`cid`="+cid+" and `pid`="+mFlow.mid+"");
        if(to>0)return "有下级不允许删除";

        int pid = Rock.getMapint(mFlow.data, "pid");
        if(pid==0)return "顶级不允许删除";

        return "";
    }
}
